import { defineStore } from "pinia";
import { Comment, Post } from "@/types/data";
import request from '@/untils/request';
import { ElMessage } from "element-plus";

const commentsStore = defineStore('comment', {
	state() {
		return {
			nowPost: {
				"postId": -1,
				"postStartUserId": -1,
				"postTitle": '',
				"postType": '',
				"postContent": '',
				"postCreateTime": '',
				"postRefNum": -1,
			},
			list: [] as Comment[],
			supList: [] as Comment[],
		}
	},
	actions: {
		async getAllCommentOfPost(id: number, type: number) {
			request.get('commentInfo/queryAll', {
				params: {
					'id': id,
					'type': type,
				}
			}).then((res: any) => {
				if (res.data.code !== 0) {
					ElMessage.error('获取失败');
					return;
				}
				if (type === 1) new Promise((resolve) => {
					this.list = [];
					resolve(true);
				}).then(() => {
					for (let i of res.data.data)
						this.list.push(i);
				});
				else new Promise((resolve) => {
					this.supList = [];
					resolve(true);
				}).then(() => {
					for (let i of res.data.data)
						this.supList.push(i);
				});
			}).catch(() => ElMessage.error('网络错误'));
		}
	}
});

export default commentsStore;
